package com.yzs.exam.controller.student;

import com.github.pagehelper.PageInfo;
import com.yzs.exam.common.base.BaseApiController;
import com.yzs.exam.common.base.RestResponse;
import com.yzs.exam.context.WebContext;
import com.yzs.exam.entity.ExamPaper;
import com.yzs.exam.entity.Question;
import com.yzs.exam.entity.Subject;
import com.yzs.exam.entity.User;
import com.yzs.exam.entity.enums.ExamPaperTypeEnum;
import com.yzs.exam.entity.enums.QuestionTypeEnum;

import com.yzs.exam.service.ExamPaperAnswerService;
import com.yzs.exam.service.ExamPaperService;
import com.yzs.exam.service.QuestionService;
import com.yzs.exam.service.SubjectService;
import com.yzs.exam.utils.DateTimeUtil;
import com.yzs.exam.utils.ExamUtil;
import com.yzs.exam.utils.PageInfoHelper;
import com.yzs.exam.viewmodel.admin.exam.ExamPaperEditRequestVM;
import com.yzs.exam.viewmodel.admin.exam.ExamPaperTitleItemVM;
import com.yzs.exam.viewmodel.admin.question.QuestionEditRequestVM;
import com.yzs.exam.viewmodel.student.exam.ExamPaperAttrVM;
import com.yzs.exam.viewmodel.student.exam.ExamPaperPageResponseVM;
import com.yzs.exam.viewmodel.student.exam.ExamPaperPageVM;
import com.yzs.exam.viewmodel.student.exam.ExamPaperRuleVM;
import io.swagger.annotations.Api;
import io.swagger.models.auth.In;
import org.apache.derby.impl.services.reflect.LoadedGeneratedClass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.annotation.Id;
import org.springframework.web.bind.annotation.*;

import javax.sound.midi.Soundbank;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Api(value = "试卷模块", tags = {"试卷接口"})
@RestController("StudentExamPaperController")
@RequestMapping(value = "/api/student/exam/paper")
public class ExamPaperController extends BaseApiController {

    private static AtomicInteger count = new AtomicInteger(0);
    private final WebContext webContext;
    private final SubjectService subjectService;
    private final ExamPaperService examPaperService;
    private final ExamPaperAnswerService examPaperAnswerService;
    private final ApplicationEventPublisher eventPublisher;

    @Autowired
    public ExamPaperController(WebContext webContext, SubjectService subjectService, ExamPaperService examPaperService, ExamPaperAnswerService examPaperAnswerService, ApplicationEventPublisher eventPublisher) {
        this.webContext = webContext;
        this.subjectService = subjectService;


        this.examPaperService = examPaperService;
        this.examPaperAnswerService = examPaperAnswerService;
        this.eventPublisher = eventPublisher;
    }


    @RequestMapping(value = "/select/{id}", method = RequestMethod.POST)
    public RestResponse<ExamPaperEditRequestVM> select(@PathVariable Integer id) {
        ExamPaperEditRequestVM vm = examPaperService.examPaperToVM(id);
        return RestResponse.ok(vm);
    }
    @RequestMapping(value = "/pageList", method = RequestMethod.POST)
    public RestResponse<PageInfo<ExamPaperPageResponseVM>> pageList(@RequestBody @Valid ExamPaperPageVM model) {
        HashMap<Integer, String> map = new HashMap<>();
        List<Subject> subjects = subjectService.allSubject();
        subjects.stream().forEach(s -> {
            map.put(s.getId(), s.getName());
        });
        PageInfo<ExamPaper> pageInfo = examPaperService.studentPage(model);
        PageInfo<ExamPaperPageResponseVM> page = PageInfoHelper.copyMap(pageInfo, e -> {
            ExamPaperPageResponseVM vm = modelMapper.map(e, ExamPaperPageResponseVM.class);
            vm.setCreateTime(DateTimeUtil.dateFormat(e.getCreateTime()));
            vm.setSubjectName(map.get(e.getSubjectId()));
            return vm;
        });
        //过滤智能组卷、或者过滤智能训练
        int paperType = model.getPaperType() == TelligentTrain.getCode() ? TelligentExam.getCode() : TelligentTrain.getCode();
        List<ExamPaperPageResponseVM> list = page.getList().stream().filter(e -> e.getPaperType() != paperType).collect(Collectors.toList());
        page.setList(list);
        return RestResponse.ok(page);
    }


    @RequestMapping(value = "/autoInsert",method = RequestMethod.POST)
    public RestResponse insert(@RequestBody ExamPaperAttrVM model){
        User user = webContext.getCurrentUser();
        ExamPaperEditRequestVM vm = examPaperService.getExamPaperEditRequestVM(model);
        examPaperService.savePaperFromVM(vm, user);
        return RestResponse.ok();
    }

    @RequestMapping(value = "/intelligence", method = RequestMethod.POST)
    public RestResponse insert(@RequestBody ExamPaperRuleVM model){
        User user = webContext.getCurrentUser();
        ExamPaperEditRequestVM vm = examPaperService.getExamPaperEditRequestVM(model);
        examPaperService.savePaperFromVM(vm, user);
        return RestResponse.ok();

    }
}
